package com.demo.controller;

import com.demo.entity.Student;
import com.demo.response.Resp;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import java.io.IOException;

/**
 * @author zhiqiang
 */
@RestController
@RequestMapping("/resetTemplateController")
@Slf4j
public class RestTemplateController {
    @Autowired
    private RestTemplate restTemplate;

    @GetMapping("/test1")
    public Resp buildStudent1() {
        ResponseEntity<String> forEntity = restTemplate.getForEntity("http://localhost:9999/ok/testController/test1/1/张三", String.class);
        return Resp.ok(forEntity.getBody());
    }

    @PostMapping("/test2")
    public Resp<Student> buildStudent2() throws IOException {
        ResponseEntity<String> entity = restTemplate.postForEntity("http://localhost:9999/ok/testController/test2?name=张三",null,String.class);
        ObjectMapper objectMapper = new ObjectMapper();
        Student student = objectMapper.readValue(entity.getBody(), Student.class);
        return Resp.ok(student);
    }
}
